var campusId = 1; //公众号id

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}


var code = getQueryString("code");
var state = getQueryString("state");


var valid1, valid2, valid3;
valid1 = valid2 = valid3 = true;

$.get("/shambles/oauth/test.do", {
    campusId: campusId
}, function (data) {
    //   alert("data="+data);

    if (data == "false") {
        //   alert("false");
        if (code != null) {
            $.get("/shambles/oauth/getUserInfo.do", {
                code: code,
                state: state,
                campusId: campusId
            }, function (text) {

                var obj = $.parseJSON(text);

                $("#brand").html(obj.campusName);
                $(".headimgUrl").attr("src", obj.headimgUrl);
                $(".nickName").html(obj.nickName);

                url = document.location.toString();

                $.ajax({
                    url: "../../wechat/sign.do",
                    data: {
                        campusId: campusId,
                        url: url
                    },
                    async: false,
                    type: "GET",
                    success: function (data) {
                        var ticket = JSON.parse(data);

                        /*
                           * 此处需要两次执行相同的函数，否则回调后不能执行以下函数
                           */

                        wx.config({
                            debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                            appId: ticket.appId,
                            timestamp: ticket.timestamp,
                            nonceStr: ticket.nonceStr,
                            signature: ticket.signature,
                            jsApiList: ['checkJsApi', 'onMenuShareTimeline',
                                'onMenuShareAppMessage', 'onMenuShareQQ',
                                'onMenuShareWeibo', 'hideMenuItems',
                                'showMenuItems', 'hideAllNonBaseMenuItem',
                                'showAllNonBaseMenuItem', 'translateVoice',
                                'startRecord', 'stopRecord', 'onRecordEnd',
                                'playVoice', 'pauseVoice', 'stopVoice',
                                'uploadVoice', 'downloadVoice', 'chooseImage',
                                'previewImage', 'uploadImage', 'downloadImage',
                                'getNetworkType', 'openLocation', 'getLocation',
                                'hideOptionMenu', 'showOptionMenu', 'closeWindow',
                                'scanQRCode', 'chooseWXPay',
                                'openProductSpecificView', 'addCard', 'chooseCard',
                                'openCard']
                        });

                    }
                });

            });
        } else {
            location.href = redirectUrl;
        }
    } else {
        $.get("/shambles/oauth/getUserInfoByOpenId.do", {
            campusId: campusId
        }, function (obj) {
            //var obj = $.parseJSON(text);
            $("#brand").html(obj.campusName);
            $(".headimgUrl").attr("src", obj.headimgUrl);
            $(".nickName").html(obj.nickName);


            url = document.location.toString();

            $.ajax({
                url: "../../wechat/sign.do",
                data: {
                    campusId: campusId,
                    url: url
                },
                async: false,
                type: "GET",
                success: function (data) {
                    //var ticket=JSON.parse(data);
                    var ticket = data;
                    /*
                   * 此处需要两次执行相同的函数，否则返回ture时不能执行以下函数
                   */


                    wx.config({
                        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                        appId: ticket.appId,
                        timestamp: ticket.timestamp,
                        nonceStr: ticket.nonceStr,
                        signature: ticket.signature,
                        jsApiList: ['checkJsApi', 'onMenuShareTimeline',
                            'onMenuShareAppMessage', 'onMenuShareQQ',
                            'onMenuShareWeibo', 'hideMenuItems',
                            'showMenuItems', 'hideAllNonBaseMenuItem',
                            'showAllNonBaseMenuItem', 'translateVoice',
                            'startRecord', 'stopRecord', 'onRecordEnd',
                            'playVoice', 'pauseVoice', 'stopVoice',
                            'uploadVoice', 'downloadVoice', 'chooseImage',
                            'previewImage', 'uploadImage', 'downloadImage',
                            'getNetworkType', 'openLocation', 'getLocation',
                            'hideOptionMenu', 'showOptionMenu', 'closeWindow',
                            'scanQRCode', 'chooseWXPay',
                            'openProductSpecificView', 'addCard', 'chooseCard',
                            'openCard']
                    });


                }
            });
        });
    }
});


wx.ready(function () {
    $('.name input').focus(function () {
        $('#w1').attr("class", "weui_cell");
    });
    /**
     * 验证姓名
     */
    $('.name input').blur(function(){
        var reque=$('.name input')[0].value;
          $.post('/shambles/mobile/register1/testName.do',{
             name:reque
             },function(text){
            data=text.data;
           if(data=='succeed'){
               valid1=true;
            }else{
                $("#dialog2").attr("style","display:block");
                $(".weui_dialog_title").html(data);
                $('#w1').attr("class","weui_cell weui_cell_warn");
                valid1=false;
             }
        });
    });

    $('.phone input').focus(function () {
        $('#w2').attr("class", "weui_cell");
    });
    /**
     * 验证电话
     */
    $('.regtlx input').blur(function(){
        var reque=$('.phone input')[0].value;
          $.post('/shambles/mobile/assetRegister/insert.do',{
              telephone:reque
             },function(text){
              var obj=JSON.parse(text);
            data=obj.data;
           if(data=='succeed'){
               valid2=true;
            }else{
                $("#dialog2").attr("style","display:block");
                $(".weui_dialog_title").html(data);
                $('#w2').attr("class","weui_cell weui_cell_warn");
                valid2=false;
             }
        });
    });
    $('.IDNo input').focus(function () {
        $('#w3').attr("class", "weui_cell");
    });
    /**
     * 查询注册信息，并且填入输入框
     */
    $.post('/shambles/mobile/assetRegister/userInfoByopenId.do', {
        campusId: campusId
    }, function (data) {
        // var obj = $.parseJSON(data);
        var obj = data;
        if (obj.UserInfo !== null) {
            console.log("1111111111111111111111"+obj.UserInfo.name)
            if (obj.UserInfo.name != null) {
                $("#name").attr("placeholder", obj.UserInfo.name);
            }
            if (obj.UserInfo.address != null) {
                $("#address").attr("placeholder", obj.UserInfo.address);
            }
            if (obj.UserInfo.phone!= null) {
                $("#phone").attr("placeholder", obj.UserInfo.phone);
            }
        }
    });
    /**
     * 修改身份信息，并且提交
     */
    document.querySelector('#showTooltips').onclick = function () {
        if (valid1 == false || valid2 == false || valid3 == false) {
            $("#dialog2").attr("style", "display:block");
            $(".weui_dialog_title").html("请修改输入错误字段");
            return;
        }
        var name = document.getElementById("name").value;
        var address = document.getElementById("address").value;
        var phone = document.getElementById("phone").value;
        var regtlx = document.getElementById("regtlx").value;
        if (campusId != null && typeof (campusId) != "undefined") {
            $.post('/shambles/mobile/assetRegister/insertUserInfo.do', {
                name: name,
                phone: phone,
                address: address,
                regtlx: regtlx
            }, function (data) {
                if (data == 1) {
                    $("#dialog2").attr("style", "display:block");
                    $(".weui_dialog_title").html("操作成功");
                    top.location.href = "index.html";
                } else if (data == 2) {
                    $("#dialog2").attr("style", "display:block");
                    $(".weui_dialog_title").html("验证码错误");
                } else if (data == 3) {
                    $("#dialog2").attr("style", "display:block");
                    $(".weui_dialog_title").html("操作失败");
                }
                refreshValidatecode();
            }, "json");
        }
    }

    var isClick = true;

    $("#bt01").click(function () {
        var name = document.getElementById("name").value;
        var phone = document.getElementById("phone").value;

        if (isClick) {
            isClick = false; //不能点击此按钮
            /* 				if (name == null || name == "") {
                                $("#dialog2").attr("style", "display:block");
                                $(".weui_dialog_title").html("请输入姓名");
                                isClick = true; //返回后能够重新点击发送的按钮
                                return false;
                            }

                            if (phone == null || phone == "") {
                                $("#dialog2").attr("style", "display:block");
                                $(".weui_dialog_title").html("请输入手机号");
                                isClick = true; //返回后能够重新点击发送的按钮
                                return false;
                            } */

            var xhm = new XMLHttpRequest();
            xhm.open("GET",
                "/shambles/mobile/register1/getValidate.do?phone="
                + phone + "&name=" + name, false);
            xhm.setRequestHeader("Content-type",
                "application/x-www-form-urlencoded");
            xhm.send();
            var json = xhm.responseText;

            if (json == 1) {

                isClick = false; //当点击后倒计时时候不能点击此按钮
                var time = 60; //倒计时60秒
                var timer = setInterval(fun1, 1000); //设置定时器

                function fun1() {
                    time--;
                    if (time >= 0) {
                        bt01.innerHTML = time + "s后重新发送";
                    } else {
                        bt01.innerHTML = "重新发送验证码";
                        isClick = true; //倒计时结束能够重新点击发送的按钮
                        clearTimeout(timer); //清除定时器
                        time = 60; //设置循环重新开始条件
                    }
                }

            }

        }
    })

}); //wx.ready